/**
 * author:Administrator
 * date:2016/3/2
 * desc:TODO
 */
"use strict";

var Router = require('koa-router');
var config = require('../../config/index.js');
var appUtil = require('../../common/appUtils.js');
let bodyParser = require('koa-better-body');
let koabody = bodyParser();

let vendorCmdService = require('../../ddd/application/vendorCmdService.js');
let vendorQueryService = require('../../ddd/application/vendorQueryService.js');

//
var __pathPrefix = '/api/v1/vendor';
appUtil.appLog('router:' + __pathPrefix + ',file=' + __filename);
var router = new Router({prefix: __pathPrefix});
module.exports = router.routes();

/**
 * 检测登录状态
 */
function checkLogin(ctx) {
  let vendor = ctx.session.vendor;
  return vendor;
}

/**
 * create a ware
 * name, categoryId, quantity, price
 */
router.post('/ware', koabody, function*() {
  // check if vendor
  let vendor = checkLogin(this);
  if (!vendor) {
    this.status = 401;
    return this.body = 'not vendor';
  }

  let name = this.request.body.fields.name;
  let categoryId = this.request.body.fields.categoryId;
  let quantity = this.request.body.fields.quantity;
  let price = this.request.body.fields.price;

  this.body = yield vendorCmdService.createWare(vendor.id, name, categoryId, quantity, price);
});

/**
 * get all ware with condition
 * get /api/v1/vendor/ware
 */
router.get('/ware', koabody, function*() {
  // check if vendor
  let vendor = checkLogin(this);
  if (!vendor) {
    this.status = 401;
    return this.body = 'not vendor';
  }

  this.body = yield vendorQueryService.wareOfVendor(vendor.id);
});

/**
 * get one ware, detailed
 */
router.get('/ware/:id', function*() {
  // check if vendor
  let vendor = checkLogin(this);
  if (!vendor) {
    this.status = 401;
    return this.body = 'not vendor';
  }
  let wareId = this.params.id;
  console.log('ware detail, wareId=', wareId);
  this.body = yield vendorQueryService.wareDetail(vendor.id, wareId);
});

/**
 * 商家:一个商品 上架到 多个商铺,
 * post /api/v1/vendor/ware/onshelf
 * wareId, shopIds,
 * 如果 shopIds=null/undefined/空数组,表示上架到该商家的全部商铺
 */
router.post('/ware/onshelf', koabody, function*() {
  // check if vendor
  let vendor = checkLogin(this);
  if (!vendor) {
    this.status = 401;
    return this.body = 'not vendor';
  }
  let wareId = this.request.body.fields.wareId;
  let shopIds = this.request.body.fields.shopIds;
  if (!shopIds) {
    let shops = yield vendorQueryService.shopOfVendor(vendor.id);
    if (shops) {
      shopIds = shops.map(aShop=>aShop.id);
    }
  }
  this.body = yield vendorCmdService.wareOnShelf(vendor.id, wareId, shopIds);
});

///**
// * 商家:改变商品属性(现在仅支持价格更改)
// * post /api/v1/vendor/ware/update
// * wareId, price,
// */
//router.put('/ware/:wareId', koabody, function*() {
//  // check if vendor
//  let vendor = checkLogin(this);
//  if (!vendor) {
//    this.status = 401;
//    return this.body = 'not vendor';
//  }
//  let wareId = this.params.wareId;
//  let attr = this.request.body.fields.attr;
//  console.log('attr=', attr);
//
//  this.body = yield vendorApi.updateWare(vendor.id, wareId, attr);
//});

/**
 * create a shop
 * name, locationId
 */
router.post('/shop', koabody, function*() {
  // check if vendor
  let vendor = checkLogin(this);
  if (!vendor) {
    this.status = 401;
    return this.body = 'not vendor';
  }
  let name = this.request.body.fields.name;
  let locationId = this.request.body.fields.locationId;

  this.body = yield vendorCmdService.createShop(vendor.id, name, locationId);
});

/**
 * get all shop
 */
router.get('/shop', koabody, function*() {
  // check if vendor
  let vendor = checkLogin(this);
  if (!vendor) {
    this.status = 401;
    return this.body = 'not vendor';
  }
  this.body = yield vendorQueryService.shopOfVendor(vendor.id);
});

/**
 * get one shop, detailed
 */
router.get('/shop/:id', function*() {
  // check if vendor
  let vendor = checkLogin(this);
  if (!vendor) {
    this.status = 401;
    return this.body = 'not vendor';
  }
  let shopId = this.params.id;
  this.body = yield vendorQueryService.shopDetail(vendor.id, shopId);
});

